﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace zj.Communicationlib.ByteArrays
{
    /// <summary>
    /// ByteArray工具类用于报文拼接
    /// </summary>
    public class ByteArray
    {
        #region 字段
        private List<byte> list = new List<byte>();
        #endregion

        #region 索引器
        public byte this[int index]                   //索引器定义
        {

            get
            {
                //处理语句
                return list[index];
            }
            set
            {
                list[index] = value;
            }
        }


        #endregion
        #region 属性


        public List<byte> List
        {
            get { return list; }
            set { list = value; }
        }
        /// <summary>
        /// 获取 Array数组
        /// </summary>
        public byte[] Array
        {
            get { return list.ToArray(); }
        }
        public int Length
        {
            get { return list.Count; }
        }
        #endregion
        #region 方法
        /// <summary>
        /// 添加一个字节
        /// </summary>
        /// <param name="item"></param>
        public void Add(byte item)
        {
            list.Add(item);
        }
        /// <summary>
        /// 添加多个字节或字节数组
        /// </summary>
        /// <param name="array"></param>
        public void AddRange(params byte[] array)
        {
            list.AddRange(array);
        }
        /// <summary>
        /// 添加字节数组中指定长度的前多个字节或字节数组
        /// </summary>
        /// <param name="array"></param>
        /// <param name="length"></param>
        public void AddRange(byte[] array,int length)
        {
            if(array.Length < length)
            {
                return;
            }
            else
            {
                for(int i = 0;i<length;i++)
                {
                    list.Add(array[i]);
                }
            }
        }
        /// <summary>
        /// 添加集合元素
        /// </summary>
        /// <param name="array"></param>
        public void Add(List<byte> array)
        {
            list.AddRange(array);
        }
        /// <summary>
        /// 添加一个对象
        /// </summary>
        /// <param name="byteArray"></param>
        public void Add(ByteArray byteArray)
        {
            AddRange(byteArray.Array);
        }
        /// <summary>
        /// 添加一个short类型
        /// </summary>
        /// <param name="value"></param>
        public void Add(short value)
        {
            Add((byte)(value >> 8));   //先装高位
            Add((byte)(value));        //再装低位
        }

        /// <summary>
        /// 添加一个ushort类型
        /// </summary>
        /// <param name="value"></param>
        public void Add(ushort value)
        {
            Add((byte)(value >> 8));
            Add((byte)(value));
        }

        public void AddRange(bool[] value)
        {
            ///组合字节数组
            int byteLength = value.Length % 8 == 0 ? value.Length / 8 : value.Length / 8 + 1;
            byte[] bytes = new byte[byteLength];
            for (int i = 0; i < value.Length; i++)
            {
                int j = i / 8;      //获取字节索引
                byte b = (byte)(value[i] ? Math.Pow(2, i - 8 * j) : 0);
                bytes[j] |= b;
            }
            AddRange(bytes);

        }
        /// <summary>
        /// 清空对象
        /// </summary>
        public void Clear()
        {
            list.Clear();
        }

        #endregion
    }
}
